/*
 SAMPLE FORM
*/

require('./common');
var gs3 = require('gs3');
var Database = gs3.Models.Database;
var Entity = gs3.Models.Entity;

var db = new Database(config, true);

exports.load = function(req, res){
	var formID = req.params.formID;
	var workflowID = req.params.wfID;
	db.selectByKey('tblworkspace', ['FormName'], 'ID', formID, function(err, data){
		if (err) {
			
		} else {
			getWorkflow(workflowID, function(instanceRef) {
				wfObjects = instanceRef;
				
				var objects = {
				title: "Workflow",
				formID:formID,
				formTitle:data[0].FormName,
				startCtr: wfObjects.startCTR,
				lines:wfObjects.lines,
				nodes:wfObjects.nodes,
			}
			res.render("workflow", objects);
			});
			
		}
	});
}
function getWorkflow(workflowID, callback){
	this.startCTR  = 1;
	this.lines = "";
	this.nodes = "";
	
	var workflowInstanceRef = this;
	
	if(workflowID!=0){
		//get nodes
		db.selectByKey('tbworkflow_obects', '*', 'workflow_id', workflowID, function(err, data){
		  if(err){
		  }else{
		    var getMax = null;
		    var objectIndex = null;
		    for(var a=0;a<data.length;a++){
		      workflowInstanceRef.nodes += data[a].object_id+"++"+data[a].tops+"++"+data[a].lefts+"++"+data[a].processorType+"++"+data[a].processor+"++"+data[a].buttonStatus+"{}";
		      
		      console.log(workflowInstanceRef.nodes);
		      
		      objectIndex = parseInt(data[a].object_id.split("_")[1]);
		      if(getMax<objectIndex){
			getMax = objectIndex;
		      }
		    }
		    workflowInstanceRef.startCTR+=getMax;
		    //get lines
		    db.selectByKey('tbworkflow_lines', '*', 'workflow_id', workflowID, function(err, datas){
		      if(err){
			
		      }else{
			for(var b=0;b<datas.length;b++){
			  workflowInstanceRef.lines+=datas[b].lineClass+"++"+datas[b].parent+"++"+datas[b].child+"++"+datas[b].line_top+"++"+datas[b].line_left+"++"+datas[b].line_height+"++"+datas[b].line_width+"{}";
			}
			
			callback(workflowInstanceRef);
		      }
		    })
		  }
		})
	}else{
		callback(workflowInstanceRef);
	}
}

exports.save = function(req, res){
	var title = req.params.workFlowTitle
	var desc = req.params.workFlowDesc
	var form_id = req.params.form_id
	var nodes = req.params.nodes; // for nodes
	var lines = req.params.lines; // for lines
	var delegateRequest = req.params.delegateRequest;
	
	var workflow = new Entity('tbworkflow', 'id');

	workflow.fields.company_id = 0;
	workflow.fields.form_id = form_id;
	workflow.fields.title = title;
	workflow.fields.description = desc;
	workflow.fields.date = "0";
	workflow.fields.delegateRequest = delegateRequest;
	workflow.fields.is_active = 1;
	
	db.insert(workflow, function(err, data) {
		if (err) {
			res.send(err.message);
		} else {
			workflow_id = data.insertId;
			insertNodes(workflow_id,nodes);
			insertLines(workflow_id,lines);
			res.send("Saved")
		}
	});
	
	/*
	
	for(a=0;a<nodes.length;a++){
		if(nodes[a]!=""){
			output+="id = "+nodes[a].split("++")[0]+
			", top = "+nodes[a].split("++")[1]+
			", left = "+nodes[a].split("++")[2]+
			", text = "+nodes[a].split("++")[3]+"\n";	
		}
	}
	
	output+="\n\n";
	
	for(c=0;c<nodes.length;c++){
		if(nodes[c]!=""){
			output+="class = "+lines[c].split("++")[0]+
			", parent = "+lines[c].split("++")[1]+
			", child = "+lines[c].split("++")[2]+
			", top = "+lines[c].split("++")[3]+
			", left = "+lines[c].split("++")[4]+
			", height = "+lines[c].split("++")[4]+
			", width = "+lines[c].split("++")[4]+"\n";	
		}
	}
	
	res.send(orgchart_id);
	*/
}

function insertNodes(workflow_id,nodes){
	var orgchartObjects = new Entity('tbworkflow_obects', 'id');
	nodes = nodes.split("{}");
	for(a=0;a<nodes.length;a++){
		if(nodes[a]!=""){
			orgchartObjects.fields.workflow_id = workflow_id;
			orgchartObjects.fields.object_id = nodes[a].split("++")[0];
			orgchartObjects.fields.tops = nodes[a].split("++")[1];
			orgchartObjects.fields.lefts = nodes[a].split("++")[2];
			orgchartObjects.fields.processorType = nodes[a].split("++")[3];
			orgchartObjects.fields.processor = nodes[a].split("++")[4];
			orgchartObjects.fields.buttonStatus = nodes[a].split("++")[5];
	
			db.insert(orgchartObjects, function(err, data) {
				if (err) {
					//res.send(err.message);
				}
			});
		}
	}
}
function insertLines(workflow_id,lines){
	var orgchartLines = new Entity('tbworkflow_lines', 'id');
	lines = lines.split("{}");
	for(a=0;a<lines.length;a++){
		if(lines[a]!=""){
			orgchartLines.fields.workflow_id = workflow_id;
			orgchartLines.fields.lineClass = lines[a].split("++")[0];
			orgchartLines.fields.parent = lines[a].split("++")[1];
			orgchartLines.fields.child = lines[a].split("++")[2];
			orgchartLines.fields.line_top = lines[a].split("++")[3];
			orgchartLines.fields.line_left = lines[a].split("++")[4];
			orgchartLines.fields.line_height = lines[a].split("++")[5];
			orgchartLines.fields.line_width = lines[a].split("++")[6];
	
			db.insert(orgchartLines, function(err, data) {
				if (err) {
					//res.send(err.message);
				}
			});
		}
	}
	
}